using System;
using System.Collections;
using System.IO;

namespace SSDS
{

    public class TextFile
    {
        protected System.Windows.Forms.IWin32Window Parent;
        public    System.Collections.ArrayList Lines;
        public    string Name;

        public void ReadFile()
        {
            StreamReader fin = new StreamReader(Name);
            do
            {
                Lines.Add(fin.ReadLine());
            } while (Lines[Lines.Count-1] != null);
            Lines.Remove(null);
        }

        public void WriteFile()
        {
            StreamWriter fout = new StreamWriter(Name, false);

            for (int i = 0; i < Lines.Count; i++)
            {
                fout.WriteLine((string)Lines[i]);
            }
            
            fout.Close();
        }


        protected TextFile(System.Windows.Forms.IWin32Window parent, string Filename)
        {
            Name   = Filename;
            Parent = parent;
            Lines  = new ArrayList();
        }


        public TextFile(System.Windows.Forms.IWin32Window parent, string filename, bool Create) 
        : this(parent, filename)
        {
            try 
            { 
                ReadFile();
            }
            catch (FileNotFoundException)
            {
                if (!Create)
                {
                    return;
                }

                Prompt WantToCreate = new Prompt();
                WantToCreate.Label.Text = string.Format("No such file. Create it Y/N <N> {0}", Name.ToUpper());
                WantToCreate.ShowDialog(Parent);
                if (WantToCreate.Entry.Text.ToUpper() != "Y")
                {
                    return;
                }

                WriteFile();
            }
        }
    }
}
